package GUI;

	
import java.awt.*;

import javax.swing.*;

import Library.Login;

import client.ChatClient;
import client.ClientConsole;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
 * the gui of the login
 * @author Maor
 *
 */
public class GUI_LogIn extends JFrame implements WindowListener{
		
	
	
	/**for getting user name*/
	private JTextField textField;
	/**for getting password*/
	private JTextField textField_1;
	/**
	 * client Connected
	 */
	private ClientConsole cc;
	/** manager menu panel*/
	private GUI_ManagerMenu mm;
	/** liberian menu panel*/
	private GUI_LibrarianMenu lm;
	private JPanel FirstPanel = null;
	private JPanel Panel = null;
	/** back panel*/
	public static GUI_LogIn forswitchPanels;
	private String userName;
	private String password;
	/**for getting the ip*/
	private JTextField textField_2;
	private static GUI_LogIn cgui;
	public static String host;

	public GUI_LogIn() {
		
		getContentPane().setLayout(null);
		this.setSize(700, 600);
		this.setContentPane(getSec());
		this.setVisible(true);
		forswitchPanels=this;
		addWindowListener(this);

		
	}
	
   
    
    /**
     * make the first panel for putting the ip of the server
     * @return this panel
     */
	public JPanel getSec() {
		if (Panel == null) {
			Panel = new JPanel();
			Panel.setLayout(null);

			JLabel lblNewLabel = new JLabel("IP");
			lblNewLabel.setBounds(80, 57, 46, 14);
			Panel.add(lblNewLabel);
			
			textField_2 = new JTextField();
			textField_2.setBounds(157, 54, 173, 20);
			Panel.add(textField_2);
			textField_2.setColumns(10);
			JButton btnNewButton = new JButton("Connect");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					cc = new ClientConsole(textField_2.getText());
					cc.cgui=cgui;
					Thread t=new Thread(cc);
					t.start();
					forswitchPanels.setContentPane(getFirstPanel());
					forswitchPanels.setVisible(true);
				}
			});
			btnNewButton.setBounds(200, 95, 89, 23);
			Panel.add(btnNewButton);
			
			
		
		}
		return Panel;
		}



	public JButton disconnect()
	{
		JButton btndis = new JButton("disconnect");
		btndis.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) 
			{
			try {
				cc.client.closeConnection();
				System.exit(0);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
				
				
			}	
		});
		btndis.setBounds(100, 94, 120, 23);
		//FirstPanel.add(btnLogIn);
		return btndis;
		
		
	}
	public JButton logInB()
	{
		JButton btnLogIn = new JButton("Log In");
		btnLogIn.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) 
			{
				/*textField.setText("55")*/
				userName=textField.getText();
				password=textField_1.getText();
				try {
					//Login user=new Login(userName,password);
					String s[]={"login",userName,password};
					cc.client.sendToServer(s);
					while(cc.client.flag==0)
					    System.out.print("");
				    cc.client.flag=0;
				    
				    System.out.print(cc.client.flag);
					Login login=(Login)cc.client.message;
					
					cc.rank=login.getRank();
					String err = null;
					switch (login.getStatus())
					{
					case 0:	// log success 
						System.out.println(login.getRank());
						switch (login.getRank())
						{
						case 0:	// Student
							cc.userName = login.getUserName();
							String s1[]={"CheckReaderCardStudent",cc.userName};
							cc.client.sendToServer(s1);
							while(cc.client.flag==0)
							    System.out.print("");
						   cc.client.flag=0;
						   int msgNum =(Integer)cc.client.message;
						    GUI_MainMenuStudent mms = new GUI_MainMenuStudent(cc,forswitchPanels,msgNum);
							cc.cgui.setContentPane(mms);
							break;
						case 1:	// Librarian
							cc.userName=login.getUserName();
							String s2[]={"msgOnExtendedBooks"};
							cc.client.sendToServer(s2);
							while(cc.client.flag==0)
							    System.out.print("");
						   cc.client.flag=0;
						   ArrayList<String[]> msgs=cc.client.details;
						   GUI_DisplayMsgs dm;
						   if(msgs.size()>=1) dm=new GUI_DisplayMsgs(msgs);
							lm=new GUI_LibrarianMenu(cc,forswitchPanels);
							setContentPane(lm);
							break;
						case 2:	// Manager
							cc.userName=login.getUserName();
							mm=new GUI_ManagerMenu(cc,forswitchPanels);
							setContentPane(mm);
						}
						break;
					case 1:	// already logged
						err = "you already logged";
						break;
					case 2:	// wrong password;
						err = "wrong password";
						break;
					case 3: // user does not exists
						System.out.print("wrong");
						err = "wrong user name";
						break;
					}
				if (err!=null)
				{
				JOptionPane.showMessageDialog(new JFrame(),err);
				}
				
				
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		btnLogIn.setBounds(244, 94, 89, 23);
		//FirstPanel.add(btnLogIn);
		return btnLogIn;
		
		
	}
	/**
	 * making the seconed panel for make the login with user name and password
	 * @return this panel
	 */
	public JPanel getFirstPanel() {
		if (FirstPanel == null) {
			FirstPanel = new JPanel();
			FirstPanel.setLayout(null);
			JLabel label = new JLabel("User Name");
			label.setBounds(10, 11, 101, 14);
			FirstPanel.add(label);
			
			JLabel lblPassword = new JLabel("Password");
			lblPassword.setBounds(10, 47, 101, 14);
			FirstPanel.add(lblPassword);
			
			textField = new JTextField();
			textField.setBounds(143, 8, 86, 20);
			FirstPanel.add(textField);
			textField.setColumns(10);
			
			textField_1 = new JTextField();
			textField_1.setBounds(143, 44, 86, 20);
			FirstPanel.add(textField_1);
			textField_1.setColumns(10);
			FirstPanel.add(logInB(), null);
			FirstPanel.add(disconnect(),null);
		}
		return FirstPanel; 
	}
	
	public void LogOut()
	{
		try {	
			String[] s={"logout",userName};
			cc.client.sendToServer(s);
			while(cc.client.flag==0)
				System.out.print("");
			cc.client.flag=0;
			textField.setText("");
			textField_1.setText("");
			setContentPane(FirstPanel);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
	
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}




	@Override
/**
 * calld when the frame closed for disconnect the client
 */
	public void windowClosing(WindowEvent e) {

		if(!forswitchPanels.getContentPane().equals(Panel))
		{
		String[] s={"logout",userName};
		try {
			cc.client.sendToServer(s);
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		while(cc.client.flag==0)
			System.out.print("");
		cc.client.flag=0;
		
		
			
		try {
			cc.client.closeConnection();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		}
		System.exit(0);
		
	}
	
	/**
	 * main- for starth the connection between the  client and the server
	 */
	public static void main(String[] args) {
		host = "";
		int port = 0; // The port number

		try {
			host = args[0];
		} catch (ArrayIndexOutOfBoundsException e) {
			host = "localhost";
		}
		 cgui=new GUI_LogIn();
		}
}



